<template>
  <div class="list-table">
    <div class="area-header">
      <span class="area-header-title">{{ $t('租金管理') }}</span>
    </div>
    <el-form ref="queryForm" :model="queryParams" :inline="true">
      <el-form-item :label="$t('房号')" prop="roomNumberName">
        <el-input
          v-model="queryParams.roomNumberName"
          :placeholder="$t('房号')"
          clearable
          @keyup.enter.native="queryHandle"
        />
      </el-form-item>
      <el-form-item :label="$t('租户')" prop="lesseeName">
        <el-input
          v-model="queryParams.lesseeName"
          :placeholder="$t('租户')"
          clearable
          @keyup.enter.native="queryHandle"
        />
      </el-form-item>
      <el-form-item :label="$t('状态')" prop="rentState">
        <el-select v-model="queryParams.rentState" :placeholder="$t('状态')" clearable>
          <el-option
            v-for="dict in rentStateOptions"
            :key="dict.dictValue"
            :label="$t(dict.dictLabel)"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item :label="$t('年月')" prop="years">
        <DatePicker v-model="daysTime" type="monthrange" :placeholder="$t('年月')" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="queryHandle">{{ $t('搜索') }}</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">{{ $t('重置') }}</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['deal:rent:export']"
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="exportHandle"
        >{{ $t('导出') }}</el-button>
      </el-col>
    </el-row>
    <el-table v-loading="loading" :data="dealLeaseRentList" border tooltip-effect="darkTable">
      <el-table-column :label="$t('序号')" type="index" align="center" show-overflow-tooltip width="50">
        <template slot-scope="scope">
          {{ (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column :label="$t('租约名称')" align="center" prop="leaseName" min-width="200" show-overflow-tooltip>
        <template slot-scope="scope">
          <el-link type="primary" @click="updateHandle(scope.row)">{{ scope.row.leaseName }}</el-link>
        </template>
      </el-table-column>
      <el-table-column :label="$t('房号')" align="center" prop="roomNumberName" min-width="200" show-overflow-tooltip>
        <template slot-scope="scope">
          {{ scope.row.roomNumberName }}
        </template>
      </el-table-column>
      <el-table-column :label="$t('租户')" align="center" prop="lesseeName" min-width="150" show-overflow-tooltip />
      <el-table-column :label="$t('租期明细')" align="center" prop="years" min-width="110">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.years, '{y}-{m}') }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('应收租金(元)')" align="center" prop="receivableRent" min-width="110">
        <template slot-scope="scope">
          <span class="money-text">{{ moneyFormat(scope.row.receivableRent) }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('实收租金(元)')" align="center" prop="paidInRent" min-width="110">
        <template slot-scope="scope">
          <span class="money-text">{{ moneyFormat(scope.row.paidInRent) }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('实收日期')" align="center" prop="paidInDate" min-width="110">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.paidInDate) }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('状态')" align="center" prop="rentState" min-width="110">
        <template slot-scope="scope">
          <span v-if="scope.row.rentState === 'WJ'" :class="rentStateColor(scope.row)">未缴</span>
          <span v-if="scope.row.rentState === 'YJ'" :class="rentStateColor(scope.row)">已缴</span>
          <span v-if="!scope.row.rentState" :class="rentStateColor(scope.row)">未缴</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('操作')" align="center" class-name="small-padding fixed-width" min-width="80">
        <template slot-scope="scope">
          <el-button
            v-hasPermi="['deal:lease:edit']"
            size="small"
            type="text"
            icon="el-icon-edit"
            @click="updateRentState(scope.row)"
          >{{ $t('维护缴款信息') }}</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改租约管理对话框 -->
    <el-dialog :title="$t(title)" :visible.sync="formOpen" :close-on-click-modal="false" :lock-scroll="false" custom-class="dialogForm" append-to-body>
      <dealLeaseForm ref="dealLeaseForm" />
    </el-dialog>
    <!-- 修改租金信息 -->
    <el-dialog :title="$t(title2)" :visible.sync="formOpen2" :close-on-click-modal="false" :lock-scroll="false" custom-class="dialogForm" append-to-body>
      <dealLeaseRentForm ref="dealLeaseRentForm" />
    </el-dialog>
  </div>
</template>

<script>
  import { findListByDealId, delDealLeaseRent } from '@/api/deal/immovable/dealLease'
  import dealLeaseForm from './dealLeaseForm.vue'
  import dealLeaseRentForm from './dealLeaseRentForm.vue'
export default {
    components: {
      dealLeaseForm,
      dealLeaseRentForm
    },
  data() {
      return {
        // 年月日期范围
        daysTime: [],
          // 状态字典
         rentStateOptions: [],
          // 遮罩层
          loading: true,
          // 总条数
          total: 0,
          // 租金明细表格数据
          dealLeaseRentList: [],
          // 弹出层标题
          title: '租约管理',
          title2: '租金管理',
          // 是否显示弹出层
          formOpen: false,
          formOpen2: false,
          // 查询参数
          queryParams: {
            roomNumberName: undefined,
            lesseeName: undefined,
              dealId: this.$route.query.id,
              leaseId: undefined,
              years: undefined,
              receivableRent: undefined,
              paidInRent: undefined,
              paidInDate: undefined,
              rentState: undefined,
              startDate: undefined,
              endDate: undefined,
              pageNum: 1,
              pageSize: 10
          },
          // 表单参数
          form: {},
          // 表单校验
          rules: {}
      }
  },
  created() {
      this.getList()
    this.getDicts('rent_state').then(response => {
      this.rentStateOptions = response.data
    })
  },
  methods: {
    /** 查询租金明细列表 */
    getList() {
        this.loading = true
      if (this.daysTime != null && this.daysTime.length > 1) {
        this.queryParams.startDate = this.parseTime(this.daysTime[0])
        this.queryParams.endDate = this.parseTime(this.daysTime[1])
      } else {
        this.queryParams.startDate = undefined
        this.queryParams.endDate = undefined
      }
      findListByDealId(this.queryParams).then(response => {
            this.dealLeaseRentList = response.rows
            this.total = response.total
            this.loading = false
        }).catch(() => {
            this.loading = false
        })
    },

    rentStateFormat(row) {
      return this.selectDictLabel(this.rentStateOptions, row.rentState)
    },

    // 表单重置
    reset() {
        this.form = {
            id: undefined,
            leaseId: undefined,
            years: undefined,
            receivableRent: undefined,
            paidInRent: undefined,
            paidInDate: undefined,
            rentState: undefined,
            delFlag: undefined,
            createBy: undefined,
            createTime: undefined,
            updateBy: undefined,
            updateTime: undefined
        }
        this.resetForm('form')
    },
    /** 搜索按钮操作 */
    queryHandle() {
        this.queryParams.pageNum = 1
        this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
        this.resetForm('queryForm')
      this.daysTime = []
        this.queryHandle()
    },
    /** 删除按钮操作 */
    deleteHandle(row) {
        this.$confirm(this.$t('是否确认删除?'), this.$t('警告'), {
            confirmButtonText: this.$t('确定'),
            cancelButtonText: this.$t('取消'),
            type: 'warning'
        }).then(() => {
            return delDealLeaseRent(row.id)
        }).then(() => {
            this.getList()
            this.msgSuccess(this.$t('删除成功'))
        }).catch(() => {
            this.loading = false
        })
    },
    // 查询租约管理页面
    updateHandle(row) {
      this.formOpen = true
      this.$nextTick(() => {
        const rows = {
          id: row.leaseId
        }
        // 调用form页面的 init 方法初始化数据
        this.$refs.dealLeaseForm.init(rows)
      })
    },
    rentStateColor(row) {
      var date = new Date(row.years)
      // 获取 下月二号
      var y = new Date().getFullYear()
      var m = new Date().getMonth() + 1
      var res = new Date(y, m, 2)
      if ((row.rentState && row.rentState === 'WJ' && res > date) || !row.rentState) {
       return 'rentStateColor'
      } else {
        return ''
      }
    },
    // 查询租金管理
    updateRentState(row) {
      this.formOpen2 = true
      this.$nextTick(() => {
        debugger
        // 调用form页面的 init 方法初始化数据
        this.$refs.dealLeaseRentForm.init(row)
      })
    },
    /** 导出按钮操作 */
    exportHandle() {
      this.downLoadExcel('/rent/dealLeaseRent/export', this.queryParams)
    }
  }
}
</script>
<style lang="scss" scoped>
  .rentStateColor {
    color: red;
  }
</style>
